package com.example.user.controller.api;

import com.example.common.util.core.controller.BaseController;
import com.example.common.util.core.resultbody.AjaxResult;
import com.example.service.util.service.TokenService;
import com.example.model.domain.entity.user.UserInfo;
import com.example.model.vo.user.LoginVo;
import com.example.model.vo.user.UserAuthVo;
import com.example.user.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/7/16 9:35
 * @Info
 */
@RestController
@RequestMapping("api/user/user")
public class UserInfoApiController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private TokenService tokenService;



    @ApiOperation("登录")
    @PostMapping("login")
    public AjaxResult login(@RequestBody LoginVo loginVo) {
        Map<String,Object> info = userInfoService.login(loginVo);
        return AjaxResult.success(info);
    }


    @ApiOperation("用户认证")
    @PostMapping("auth/user")
    public AjaxResult userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        return toAjax(userInfoService.userAuth(tokenService.getUserId(request),userAuthVo));
    }


    //获取用户信息
    @ApiOperation("获取用户信息")
    @GetMapping("auth/get/userInfo")
    public AjaxResult getUserInfo(HttpServletRequest request) {
        Long userId = tokenService.getUserId(request);
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        return AjaxResult.success(userInfo);
    }


}
